__all__ = ['InflowBoundary','OutwardBoundaryVector']

from dolfin import SubDomain,Expression,Cell
from numpy import dot

class InflowBoundary(SubDomain):
    '''Identify inflow boundary based on velocity field.'''
    def __init__(self,n,v):
        self.n = n
        self.v = v
        SubDomain.__init__(self)

    def inside(self, x, on_boundary):
        return on_boundary and dot(self.n(x), self.v(x)) < 0

class OutwardBoundaryVector(Expression):
    '''Fill values with normal components (nx,ny).'''
    def __init__(self,mesh):
        self.mesh = mesh
        Expression.__init__(self)

    def eval_cell(self, values, x, ufc_cell):
        cell = Cell(self.mesh, ufc_cell.index)
        n = cell.normal(ufc_cell.local_facet)
        values[0] = n.x()
        values[1] = n.y()

    def value_shape(self):
        return (2,)


